// pages/outvisit/task/taskDetails.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
var common = require("../../../utils/common.js");
const pagetitle = ["", "任务详情", "任务详情", "任务详情", "任务详情"];
const taskDetailsTitles = common.taskDetailsTitles;//详情字段
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,//用户ID
    type: 0,//任务类型
    id: 0,//任务ID
    completeState:{
      checkinStatus: '',
      uploadImageOutterStatus: '',
      uploadImageInnerStatus: '',
      uploadImageSceneStatus: '',
      uploadVedioStatus: '',
      addCaseStatus: '',
      uploadAttachmentStatus: '',
      priceInfoStatus: '',
      landInfoStatus: '',
      decorationInfoStatus: '',
      equityInfoStatus: '',
      inputBasicInfoStatus: '',
      locationInfoStatus: '',
    },
    surveyCase: {},
    taskDetailsIsHide:true,//任务详情是否隐藏
    taskDetailsList: [],//任务详情列表
    causeState:1,//外访信息状态
    operationIsHide:true,//下方操作按钮是否隐藏
    stopOutvisitFormIsHide:true,//终止任务表单是否隐藏
    suveyColumnList: [],
    detail: {},//查勘详情
    busname:'',
    orderNumber: '3',
    uploadImageSceneStatus: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var id = options.id;
    var busname = options.busname;

    // 设置任务类型和ID
    that.setData({
      userId: userId,
      id: id,
      busname: busname,
    }, () => {
      // 获取任务详情
      that.getSurveyDetails();
      that.getSurveyColumnComplete();
      that.getSurveyCaseInfoForApp();
      that.getUploadImageSceneStatus();
    });
  },

  getUploadImageSceneStatus:function () {
    var that = this;
    api.getSurveyImgs(that.data.id,"案例3现场照片",'').then(res => {
      if (res.code == 0) {
        var data = res.data;

        if (data.length > 0) {
          // 更新数据
          that.setData({
            uploadImageSceneStatus: '已完成'
          }, () => {
            //...
          });
        }
        else {
          // 更新数据
          that.setData({
            uploadImageSceneStatus: '未完成'
          }, () => {
            //...
          });
        }
      }
    });
  },

  getSurveyCaseInfoForApp:function() {
    var that = this;
    api.getSurveyCaseInfoForApp(that.data.id,that.data.orderNumber).then(res => {
      if (res.code == 0) {
        var data = res.data;
         

        // 更新数据
        that.setData({
          surveyCase: data,
        }, () => {
        });
      }
    });
  },

  // 获取任务详情
  getSurveyDetails:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getAppointmentDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();

        var data = res.data;
        // 更新数据
        that.setData({
          detail: data,
        }, () => {
          //console.log("busname==================>"+that.data.busname);
        });
      }
    });
  },

  // 获取任务详情
  getSurveyColumnComplete:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getSuveyColumnCompleteStatus(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
        var data = res.data;

        // 更新数据
        that.setData({
          completeState: data,
        }, () => {
          //...
        });
      }
    });
  },

  //现场照片
  uploadImageScene: function () {
    var that = this;

    var url = 'uploadImageSceneForCase3?id=' + that.data.id +'&order=3';

    console.log(url);
    wx.navigateTo({
      url: url
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var that = this;
    console.log("到提交方法了");
    if (that.data.isNoUpdate){
      wx.showModal({
        title: "提示",
        content: "确定要添加案例吗？",
        success: function (sm) {
          if (sm.confirm) {
            // 提交表单数据
            that.formSubmitData(e);
          }
        }
      })
    } else {
      // 提交表单数据
      that.formSubmitData(e);
    }
  },

  formSubmitData: function (e) {
    var that = this;
    var data = e.detail.value;
    var submitData = {};
    for (var d in data) {
      submitData[d] = data[d];
    }

    that.getUploadImageSceneStatus();
    if (that.data.uploadImageSceneStatus == '未完成') {
      wx.showToast({
        title: '[现场拍照]必须拍照，请先拍照！',
        icon: 'none'
      });

      return;
    }

    if (submitData['address'] == null || submitData['address'] == '' || submitData['address'].length == 0) {
      wx.showToast({
        title: '[案例地址]必填，请填写！',
        icon: 'none'
      });

      return;
    }
    if (submitData['area'] == null || submitData['area'] == '' || submitData['area'].length == 0) {
      wx.showToast({
        title: '[面积]必填，请填写！',
        icon: 'none'
      });

      return;
    }
    if (submitData['monthRent'] == null || submitData['monthRent'] == '' || submitData['monthRent'].length == 0) {
      wx.showToast({
        title: '[月租金]必填，请填写！',
        icon: 'none'
      });

      return;
    }
    if (submitData['evaluateprice'] == null || submitData['evaluateprice'] == '' || submitData['evaluateprice'].length == 0) {
      wx.showToast({
        title: '[单价]必填，请填写！',
        icon: 'none'
      });

      return;
    }

    wx.showLoading({
      title: '提交中'
    });//显示加载中
    api.addCaseBaseInfoForApp(submitData).then(res => {
      wx.hideLoading();
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
          icon: "none"
        });

        let pages = getCurrentPages();//当前页面
        let prevPage = pages[pages.length - 2];//上一页面
        prevPage.getSurveyColumnComplete();

        // 返回
        wx.navigateBack({
          delta: 1,
        });
      }
    });
  },

  // 设置下方操作按钮是否隐藏
  setOperationIsHide: function (type, outvisitTime) {
    var that = this;
    var operationIsHide = true;
    if (type == 1 || type == 2) {
      operationIsHide = false;
    }
    if (type == 3) {
      var myDate = Date.parse(new Date());
      var curTime = util.formatTimeTT(myDate, "Y-M-D");
      if (outvisitTime == curTime){
        operationIsHide = false;
      }
    }
    that.setData({
      operationIsHide: operationIsHide
    });
  },

  // 完成任务
  completeOutvisit: function () {
    var that = this;
    wx.showModal({
      title: "提示",
      content: "确定要完成任务吗？",
      success: function (sm) {
        if (sm.confirm) {
          api.completeOutvisit(that.data.id);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})